#include "ds18b20.h"
#include "display.h"
//??18B20??
/*******************************************************************************
* ? ? ?         : ds18b20_init
* ????		   : IO?????????
*******************************************************************************/
void ds18b20_init()
{
    rcu_periph_clock_enable(RCU_GPIOA); // RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
    gpio_init(GPIOA, GPIO_MODE_OUT_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_8);
}

/*******************************************************************************
* ? ? ?         : ds18b20_Rst
* ????		   : DS18B20???/????
*******************************************************************************/
void ds18b20_Rst()
{
    DQOUTINT;//??
    ds18b20_dq_L;
    delay_us ( 480 ); //??480??
    ds18b20_dq_H;
    delay_us ( 480 ); //??480??
}
/*******************************************************************************
* ? ? ?         : ds18b20wr
* ????		   : DS18B20?????
* ?    ?         : dat
*******************************************************************************/
void ds18b20wr ( u8 dat )
{
    u8 i = 0;
    DQOUTINT;//??
    for ( i = 0; i < 8; i++ )
    {
        ds18b20_dq_L;	 //??
        delay_us ( 15 ); //??15??

        if ( ( dat & 0x01 ) == 1 )
        {	ds18b20_dq_H;}
        else
        {	ds18b20_dq_L;}
        delay_us ( 60 ); //??60??
        ds18b20_dq_H;
        dat >>= 1; //??????????
    }
}

/*******************************************************************************
* ? ? ?         : DS18b20rd
* ????		   : DS18B20?????
* ?    ?         : value
*******************************************************************************/
u8 DS18b20rd()
{
    u8 i = 0, value = 0;
    for ( i = 0; i < 8; i++ )
    {
        value >>= 1;
        DQOUTINT;//??
        ds18b20_dq_L;	 //??
        delay_us ( 4 ); //??4??
        ds18b20_dq_H;
        delay_us ( 10 ); //??10??

        DQININT;	 //????
        if ( gpio_input_bit_get ( GPIO_ds18b20, dq ) == 1 )
        {  value |= 0x80;} //??? ?????

        delay_us ( 45 ); //??45??
    }
    return value;
}
/*******************************************************************************
* ? ? ?         : readtemp
* ????		   : DS18B2????????????
* ?    ?         : value
*******************************************************************************/
double readtemp()			  //??????????
{
    u16 temp;
    u8 a, b;
    float value;
    ds18b20_Rst();		//?????
    ds18b20wr ( 0xcc ); //????ROM??
    ds18b20wr ( 0x44 ); //????????
    delay_ms ( 100 );
    ds18b20_Rst();	   //?????
    ds18b20wr ( 0xcc ); //????ROM??
    ds18b20wr ( 0xbe ); //???????
    a = DS18b20rd();	 //??????
    b = DS18b20rd();	 //??????
    temp = b;
    temp = ( temp << 8 ) + a;
    if ( ( temp & 0xf800 ) == 0xf800 ) //?????
    {
        temp = ( ~temp ) + 1;
        value = temp * ( -0.0625 );
    }
    else
    {	value = temp * 0.0625;}
    return value;
}

void Disp_Temperature ( u8 minus, u16 temp )
{
    disp.WriteData ( 18, 0x00 );
    if ( minus == 0 )
    {
        disp.WriteData ( 10, 0x00 );
        disp.WriteData ( 9, 0x00 );
        if ( temp >= 100 && temp <= 999 )
        {
            disp.WriteData ( 8, 0x00 );
            disp.WriteData ( 7, 0x00 );
            disp.OneNum ( 3, 0, temp / 100 % 10 );
            disp.OneNum ( 4, 1, temp / 10 % 10 );
            disp.OneNum ( 5, 0, temp % 10 );
        }
        if ( temp >= 10 && temp <= 99 )
        {
            disp.WriteData ( 8, 0x00 );
            disp.WriteData ( 7, 0x00 );
            disp.WriteData ( 6, 0x00 );
            disp.WriteData ( 5, 0x00 );
            disp.OneNum ( 4, 1, temp / 10 % 10 );
            disp.OneNum ( 5, 0, temp % 10 );
        }
        if ( temp >= 1000 && temp <= 9999 )
        {
            disp.OneNum ( 2, 0, temp / 1000 % 10 );
            disp.OneNum ( 3, 0, temp / 100 % 10 );
            disp.OneNum ( 4, 1, temp / 10 % 10 );
            disp.OneNum ( 5, 0, temp % 10 );
        }
    }
    if ( minus == 1 )
    {
        disp.WriteData ( 10, 0x00 );
        disp.WriteData ( 9, 0x00 );
        temp = ~temp + 1;
        if ( temp >= 10 && temp <= 99 )
        {
            disp.WriteData ( 8, 0x00 );
            disp.WriteData ( 7, 0x00 );
            disp.WriteData ( 6, 0x02 ); //???-? ??
            disp.WriteData ( 5, 0x00 );
            disp.OneNum ( 4, 1, temp / 10 % 10 );
            disp.OneNum ( 5, 0, temp % 10 );
        }
        if ( temp >= 100 && temp <= 999 )
        {
            disp.WriteData ( 8, 0x02 ); //???-? ??
            disp.WriteData ( 7, 0x00 );
            disp.OneNum ( 3, 0, temp / 100 % 10 );
            disp.OneNum ( 4, 1, temp / 10 % 10 );
            disp.OneNum ( 5, 0, temp % 10 );
        }

    }
    disp.WriteData ( 13, 0x03 );
    disp.WriteData ( 14, 0x03 );
    disp.OneNum ( 7, 0, 'c' );
}


